//
//  SDAnswerTestResultData.m
//
//  Created by   on 2019/3/12
//  Copyright (c) 2019 __MyCompanyName__. All rights reserved.
//

#import "SDAnswerTestResultData.h"
#import "SDAnswerTestResultProductList.h"


NSString *const kSDAnswerTestResultDataFeature = @"feature";
NSString *const kSDAnswerTestResultDataOption = @"option";
NSString *const kSDAnswerTestResultDataProductList = @"productList";
NSString *const kSDAnswerTestResultDataDesc = @"desc";


@interface SDAnswerTestResultData ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation SDAnswerTestResultData

@synthesize feature = _feature;
@synthesize option = _option;
@synthesize productList = _productList;
@synthesize desc = _desc;


+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict {
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict {
    self = [super init];
    
    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    if (self && [dict isKindOfClass:[NSDictionary class]]) {
            self.feature = [self objectOrNilForKey:kSDAnswerTestResultDataFeature fromDictionary:dict];
            self.option = [self objectOrNilForKey:kSDAnswerTestResultDataOption fromDictionary:dict];
    NSObject *receivedSDAnswerTestResultProductList = [dict objectForKey:kSDAnswerTestResultDataProductList];
    NSMutableArray *parsedSDAnswerTestResultProductList = [NSMutableArray array];
    
    if ([receivedSDAnswerTestResultProductList isKindOfClass:[NSArray class]]) {
        for (NSDictionary *item in (NSArray *)receivedSDAnswerTestResultProductList) {
            if ([item isKindOfClass:[NSDictionary class]]) {
                [parsedSDAnswerTestResultProductList addObject:[SDAnswerTestResultProductList modelObjectWithDictionary:item]];
            }
       }
    } else if ([receivedSDAnswerTestResultProductList isKindOfClass:[NSDictionary class]]) {
       [parsedSDAnswerTestResultProductList addObject:[SDAnswerTestResultProductList modelObjectWithDictionary:(NSDictionary *)receivedSDAnswerTestResultProductList]];
    }

    self.productList = [NSArray arrayWithArray:parsedSDAnswerTestResultProductList];
            self.desc = [self objectOrNilForKey:kSDAnswerTestResultDataDesc fromDictionary:dict];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation {
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.feature forKey:kSDAnswerTestResultDataFeature];
    [mutableDict setValue:self.option forKey:kSDAnswerTestResultDataOption];
    NSMutableArray *tempArrayForProductList = [NSMutableArray array];
    
    for (NSObject *subArrayObject in self.productList) {
        if ([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
            // This class is a model object
            [tempArrayForProductList addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
        } else {
            // Generic object
            [tempArrayForProductList addObject:subArrayObject];
        }
    }
    [mutableDict setValue:[NSArray arrayWithArray:tempArrayForProductList] forKey:kSDAnswerTestResultDataProductList];
    [mutableDict setValue:self.desc forKey:kSDAnswerTestResultDataDesc];

    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description  {
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict {
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}


#pragma mark - NSCoding Methods

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super init];

    self.feature = [aDecoder decodeObjectForKey:kSDAnswerTestResultDataFeature];
    self.option = [aDecoder decodeObjectForKey:kSDAnswerTestResultDataOption];
    self.productList = [aDecoder decodeObjectForKey:kSDAnswerTestResultDataProductList];
    self.desc = [aDecoder decodeObjectForKey:kSDAnswerTestResultDataDesc];
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{

    [aCoder encodeObject:_feature forKey:kSDAnswerTestResultDataFeature];
    [aCoder encodeObject:_option forKey:kSDAnswerTestResultDataOption];
    [aCoder encodeObject:_productList forKey:kSDAnswerTestResultDataProductList];
    [aCoder encodeObject:_desc forKey:kSDAnswerTestResultDataDesc];
}

- (id)copyWithZone:(NSZone *)zone {
    SDAnswerTestResultData *copy = [[SDAnswerTestResultData alloc] init];
    
    
    
    if (copy) {

        copy.feature = [self.feature copyWithZone:zone];
        copy.option = [self.option copyWithZone:zone];
        copy.productList = [self.productList copyWithZone:zone];
        copy.desc = [self.desc copyWithZone:zone];
    }
    
    return copy;
}


@end
